package com.demo.controller;

import com.demo.common.Result;
import com.demo.pojo.entity.ClazzTeacher;
import com.demo.service.ClazzTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 班级-教师关联控制器
 */
@RestController
@RequestMapping("/api/clazz-teacher")
@CrossOrigin
public class ClazzTeacherController {
    
    @Autowired
    private ClazzTeacherService clazzTeacherService;
    
    @PostMapping("/add")
    public Result<Boolean> addClazzTeacher(@RequestBody ClazzTeacher clazzTeacher) {
        try {
            boolean result = clazzTeacherService.addClazzTeacher(clazzTeacher);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("添加失败: " + e.getMessage());
        }
    }
    
    @PostMapping("/add-batch")
    public Result<Boolean> addClazzTeachers(
            @RequestParam Long clazzId,
            @RequestParam List<Long> teacherIds,
            @RequestParam(defaultValue = "1") Integer roleType) {
        try {
            boolean result = clazzTeacherService.addClazzTeachers(clazzId, teacherIds, roleType);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("批量添加失败: " + e.getMessage());
        }
    }
    
    @DeleteMapping("/delete/{id}")
    public Result<Boolean> deleteClazzTeacher(@PathVariable Long id) {
        try {
            boolean result = clazzTeacherService.deleteClazzTeacher(id);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("删除失败: " + e.getMessage());
        }
    }
    
    @DeleteMapping("/delete-by-clazz/{clazzId}")
    public Result<Boolean> deleteByClazzId(@PathVariable Long clazzId) {
        try {
            boolean result = clazzTeacherService.deleteByClazzId(clazzId);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("删除失败: " + e.getMessage());
        }
    }
    
    @DeleteMapping("/delete-by-teacher/{teacherId}")
    public Result<Boolean> deleteByTeacherId(@PathVariable Long teacherId) {
        try {
            boolean result = clazzTeacherService.deleteByTeacherId(teacherId);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("删除失败: " + e.getMessage());
        }
    }
    
    @PutMapping("/update")
    public Result<Boolean> updateClazzTeacher(@RequestBody ClazzTeacher clazzTeacher) {
        try {
            boolean result = clazzTeacherService.updateClazzTeacher(clazzTeacher);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("更新失败: " + e.getMessage());
        }
    }
    
    @GetMapping("/teachers/{clazzId}")
    public Result<List<Long>> getTeachersByClazzId(@PathVariable Long clazzId) {
        try {
            List<Long> teacherIds = clazzTeacherService.getTeachersByClazzId(clazzId);
            return Result.success(teacherIds);
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }
    
    @GetMapping("/clazzes/{teacherId}")
    public Result<List<Long>> getClazzesByTeacherId(@PathVariable Long teacherId) {
        try {
            List<Long> clazzIds = clazzTeacherService.getClazzesByTeacherId(teacherId);
            return Result.success(clazzIds);
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }
    
    @GetMapping("/teacher")
    public Result<Long> getTeacherByClazzIdAndRole(
            @RequestParam Long clazzId,
            @RequestParam Integer roleType) {
        try {
            Long teacherId = clazzTeacherService.getTeacherByClazzIdAndRole(clazzId, roleType);
            return Result.success(teacherId);
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }
}

